<template>
	<page-meta>
	    <navigation-bar
			title="设备信息维护--编辑"
			background-color="#EEEEEE"
			front-color="#ffffff "
	    />
	</page-meta>
	<view class="uni-padding-wrap">
		<view class="headerTitle">设备录入</view>
		<button type="primary" @click = "infoMoreUpd" style="margin-top: 10px;">{{updBtnStr}}</button>
		<form @submit="formSubmit" @reset="formReset">
			<view class = "moudlBox">管理信息</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">资材编号</view>
				<input class="uni-input" :disabled = "disabledStr" name="etCode" v-model="etCode" placeholder="请输入资材编号" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">资材大类编号</view>
				<input class="uni-input" :disabled = "true" name="domain" v-model="domain" placeholder="请输入资材大类编号" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">资材小类编号</view>
				<input class="uni-input" :disabled = "true" name="systemName" v-model="systemName" placeholder="请输入资材小类编号" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">设备类型</view>
				<input class="uni-input" :disabled = "true" name="etType" v-model="etType" placeholder="请输入设备类型" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">资材名称</view>
				<input class="uni-input" :disabled = "disabledStr" name="etName" v-model="etName" placeholder="请输入资材名称" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">设备描述</view>
				<input class="uni-input" :disabled = "disabledStr" name="etComment" v-model="etComment" placeholder="请输入设备描述" maxlength="150"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">创建人编号</view>
				<input class="uni-input" :disabled = "disabledStr" name="createdId" v-model="createdId" placeholder="请输入创建人编号" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">创建人姓名</view>
				<input class="uni-input" :disabled = "disabledStr" name="createdName" v-model="createdName" placeholder="请输入创建人姓名" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">创建时间</view>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							当前选择
						</view>
						<view class="uni-list-cell-db">
							<picker mode="date" :disabled = "disabledStr" :value="createdTime" :start="startDate" :end="endDate" @change="createdTimeChange">
								<view class="uni-input">{{createdTime}}</view>
							</picker>
						</view>
					</view>
				</view>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">所属单位</view>
				<input class="uni-input" :disabled = "disabledStr" name="owner" v-model="owner" placeholder="请输入所属单位" maxlength="30"/>
			</view>
			<view class = "moudlBox">基础信息</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">规格型号</view>
				<input class="uni-input" :disabled = "disabledStr" name="etModel" v-model="etModel" placeholder="请输入规格型号" maxlength="50"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">供应商</view>
				<input class="uni-input" :disabled = "disabledStr" name="etSupplier" v-model="etSupplier" placeholder="请输入供应商" maxlength="50"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">制作商</view>
				<input class="uni-input" :disabled = "disabledStr" name="etProducer" v-model="etProducer" placeholder="请输入制作商" maxlength="50"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">出厂日期</view>
				<!-- <input class="uni-input" :disabled = "disabledStr" name="dateOfProduction" v-model="dateOfProduction" placeholder="请输入制作商" /> -->
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							当前选择
						</view>
						<view class="uni-list-cell-db">
							<picker mode="date" :disabled = "disabledStr" :value="dateOfProduction" :start="startDate" :end="endDate" @change="dateOfProductionDateChange">
								<view class="uni-input">{{dateOfProduction}}</view>
							</picker>
						</view>
					</view>
				</view>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">保修期至</view>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							当前选择
						</view>
						<view class="uni-list-cell-db">
							<picker mode="date" :disabled = "disabledStr" :value="guaranteePeriod" :start="startDate" :end="endDate" @change="guaranteePeriodDateChange">
								<view class="uni-input">{{guaranteePeriod}}</view>
							</picker>
						</view>
					</view>
				</view>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">产品序列号</view>
				<input class="uni-input" :disabled = "disabledStr" name="serialNum" v-model="serialNum" placeholder="请输入产品序列号" maxlength="11"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">专业类别编号</view>
				<input class="uni-input" :disabled = "disabledStr" name="professionalId" v-model="professionalId" placeholder="请输入专业类别编号" maxlength="11"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">额定电压</view>
				<input class="uni-input" :disabled = "disabledStr" name="voltage" v-model="voltage" placeholder="请输入额定电压" maxlength="100"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">额定功率</view>
				<input class="uni-input" :disabled = "disabledStr" name="power" v-model="power" placeholder="请输入额定功率" maxlength="100"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">使用寿命</view>
				<input class="uni-input" :disabled = "disabledStr" name="life" v-model="life" placeholder="请输入使用寿命" maxlength="50"/>
			</view>
			<view class = "moudlBox">安装信息</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">线路编号</view>
				<input class="uni-input" :disabled = "true" name="lineId" v-model="lineId" placeholder="请输入线路编号" maxlength="11"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">所属站点</view>
				<input class="uni-input" :disabled = "disabledStr" name="stationName" v-model="stationName" placeholder="请输入所属站点" maxlength="30"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">安装位置</view>
				<input class="uni-input" :disabled = "disabledStr" name="installLocation" v-model="installLocation" placeholder="请输入安装位置" maxlength="50"/>
			</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">安装日期</view>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							当前选择
						</view>
						<view class="uni-list-cell-db">
							<picker mode="date" :disabled = "disabledStr" :value="installDate" :start="startDate" :end="endDate" @change="installDateChange">
								<view class="uni-input">{{installDate}}</view>
							</picker>
						</view>
					</view>
				</view>
			</view>
			<view class = "moudlBox">维护信息</view>
			<view class="uni-form-item uni-column">
				<view class="uni-title uni-common-pl">是否维修计划</view>
				<radio-group :disabled = "disabledStr" name="isPlan" @change="isPlanChange">
					<label>
						<radio value="1" :checked="planBlea" :disabled = "true"/><text>是</text>
					</label>
					<label>
						<radio value="0" :checked="!planBlea" :disabled = "true"/><text>否</text>
					</label>
				</radio-group>
			</view>
			<view class="uni-form-item uni-column" v-show="flag">
				<view class="uni-title uni-common-pl">首次维修计划日期</view>
				<input class="uni-input" :disabled = "true" name="maintainFristTime" v-model="maintainFristTime"/>
			</view>
			<view class="uni-form-item uni-column" v-show="flag">
				<view class="uni-title uni-common-pl">下次维护计划日期</view>
				<input class="uni-input" :disabled = "true" name="maintainNextTime" v-model="maintainNextTime"/>
			</view>
			<view class="uni-form-item uni-column" v-show="flag">
				<view class="uni-title uni-common-pl">距离下次维修计划间隔（天）</view>
				<input class="uni-input" :disabled = "true" name="intervalDay" v-model="intervalDay"/>
			</view>
			<view class="uni-form-item uni-column" v-show="flag">
				<view class="uni-title uni-common-pl">距离下次维护计划时间（天）</view>
				<input class="uni-input" :disabled = "true" name="maintainNextDay" v-model="maintainNextDay"/>
			</view>
			<view class="uni-btn-v">
				<button form-type="submit" type='primary'>更新</button>
				<button type="default" form-type="reset">清空</button>
			</view>
		</form>
	</view>
</template>

<style>
	page{
		background-color: #F7F7F7;
	}
	.uni-form-item .title {
		padding: 20rpx 0;
	}
	.headerTitle{
		font-size: 18px;
		text-align: center;
	}
	.extraInfo {
		font-size: 24rpx;
		color:#666666;
		margin-top: 0px;
	}
	.headerQuestion{
		font-size: 18px;
	}
	.headerDesc{
		color:#7A7E83;
		font-size: 16px;
	}
	.headerRedDesc{
		color:red;
		font-size: 16px;
		display: inline;
	}

	.manageIn {
		position: absolute;
		top: 10rpx;
		right: 30rpx;
		color: #007aff;
	}
	.moudlBox{
		font-size: 20px;
		padding-top: 10px;
		color: #0A98D5;
	}
</style>
<script>
	var graceCheckerJs = require('common/graceChecker.js');
	import tableTitleConst from '../../../common/tableTitleConst.js'
	import userInfoConst from '../../../common/userInfoConst.js'
	// import {
	// 	mapState
	// } from 'vuex';
	export default {
		
		data() {
			const currentDate = this.getDate({
			   format: true
		   })
			return {
				updBtnStr:"详情编辑",
				disabledStr:true,
				domain:"",
				systemName:"",
				createdId:userInfoConst.uid,
				createdName:userInfoConst.createUserName,
				createdTime:"",
				dateOfProduction:"",
				etName:"",
				etCode:"",
				lineId:"12",
				stationName:"",
				etModel:"",
				etType:"",
				etComment:"",
				maintain_way:"",
				etSupplier:"",
				etProducer:"",
				guaranteePeriod:"",
				serialNum:"",
				installLocation:"",
				installDate:"",
				professionalId:"",
				owner:"",
				voltage:"",
				power:"",
				life:"",
				isPlan:"",
				planBlea:false,
				maintainFristTime:"",
				maintain_this_time:"",
				maintainNextTime:"",
				intervalDay:"",
				maintainNextDay:"",
				eid:"",
				title: '首次维修计划日期',
				array: ['中国', '美国', '巴西', '日本'],
				index: 0,
				guaranteePeriod_date:currentDate,
				flag:true
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad: async function(option) {
			var eid = option.eid;
			
			
			await this.$myRequest({
				url:"/equipmentLAIM/getTableCodeAndValue/"+eid,
			}).then((res) => {
				if(res.code == "success"){
					var lineInfoList = res.mcAttrList[0].pzLine;
					var etMaintainPlanList = res.mcAttrList[0].etMaintainPlan;
					if(lineInfoList!=null){this.lineId = lineInfoList.lineId;}
					if(etMaintainPlanList!=null){
						this.intervalDay = etMaintainPlanList.intervalDay;
						this.isPlan = etMaintainPlanList.isPlan;
						this.maintain_way = etMaintainPlanList.maintainWay;
						this.maintainFristTime = etMaintainPlanList.maintainfirstTime;
						this.maintain_this_time = etMaintainPlanList.maintainthisTime;
						this.maintainNextTime = etMaintainPlanList.maintainnextTime;
						this.maintainNextDay = etMaintainPlanList.maintainnextDay;
					}
					if(this.isPlan == 1){
						this.planBlea = true;
						this.flag = true;
					}else{
						this.planBlea = false;
						this.flag = false;
					}
					// isPlanChange();
					var mcAttrInfoList = res.mcAttrList[0];
					this.eid = mcAttrInfoList.eid;
					this.createdId = userInfoConst.uid;
					this.createdName = userInfoConst.createUserName;
					this.createdTime = mcAttrInfoList.createdTime;
					this.dateOfProduction = mcAttrInfoList.dateOfProduction;
					this.domain = mcAttrInfoList.domain;
					this.etCode = mcAttrInfoList.etCode;
					this.etComment = mcAttrInfoList.etComment;
					this.etModel = mcAttrInfoList.etModel;
					this.etName = mcAttrInfoList.etName;
					this.etProducer = mcAttrInfoList.etProducer;
					this.etSupplier = mcAttrInfoList.etSupplier;
					this.etType = mcAttrInfoList.etType;
					this.guaranteePeriod = mcAttrInfoList.guaranteePeriod;
					this.installDate = mcAttrInfoList.installDate
					this.installLocation = mcAttrInfoList.installLocation;
					this.life = mcAttrInfoList.life;
					this.owner = mcAttrInfoList.owner;
					this.power = mcAttrInfoList.power;
					this.professionalId = mcAttrInfoList.professionalId;
					this.serialNum = mcAttrInfoList.serialNum;
					this.stationName = mcAttrInfoList.stationName;
					this.systemName = mcAttrInfoList.systemName;
					this.voltage = mcAttrInfoList.voltage;
				}else{
					uni.showToast({
						title:"数据列表请求失败...",
						icon:"none"
					});
				}
			});
			
			// await uni.request({
			// 	url: "http://127.0.0.1:10100/equipmentLAIM/getTableCodeAndValue/"+eid,
			// 	success: (res) => {
			// 		if(res.data.code == "success"){
			// 			var lineInfoList = res.data.mcAttrList[0].pzLine;
			// 			var etMaintainPlanList = res.data.mcAttrList[0].etMaintainPlan;
			// 			if(lineInfoList!=null){this.lineId = lineInfoList.lineId;}
			// 			if(etMaintainPlanList!=null){
			// 				this.intervalDay = etMaintainPlanList.intervalDay;
			// 				this.isPlan = etMaintainPlanList.isPlan;
			// 				this.maintain_way = etMaintainPlanList.maintainWay;
			// 				this.maintainFristTime = etMaintainPlanList.maintainfirstTime;
			// 				this.maintain_this_time = etMaintainPlanList.maintainthisTime;
			// 				this.maintainNextTime = etMaintainPlanList.maintainnextTime;
			// 				this.maintainNextDay = etMaintainPlanList.maintainnextDay;
			// 			}
			// 			if(this.isPlan == 1){
			// 				this.planBlea = true;
			// 				this.flag = true;
			// 			}else{
			// 				this.planBlea = false;
			// 				this.flag = false;
			// 			}
			// 			// isPlanChange();
			// 			var mcAttrInfoList = res.data.mcAttrList[0];
			// 			this.eid = mcAttrInfoList.eid;
			// 			this.createdId = mcAttrInfoList.createdId;
			// 			this.createdName = mcAttrInfoList.createdName;
			// 			this.createdTime = mcAttrInfoList.createdTime;
			// 			this.dateOfProduction = mcAttrInfoList.dateOfProduction;
			// 			this.domain = mcAttrInfoList.domain;
			// 			this.etCode = mcAttrInfoList.etCode;
			// 			this.etComment = mcAttrInfoList.etComment;
			// 			this.etModel = mcAttrInfoList.etModel;
			// 			this.etName = mcAttrInfoList.etName;
			// 			this.etProducer = mcAttrInfoList.etProducer;
			// 			this.etSupplier = mcAttrInfoList.etSupplier;
			// 			this.etType = mcAttrInfoList.etType;
			// 			this.guaranteePeriod = mcAttrInfoList.guaranteePeriod;
			// 			this.installDate = mcAttrInfoList.installDate
			// 			this.installLocation = mcAttrInfoList.installLocation;
			// 			this.life = mcAttrInfoList.life;
			// 			this.owner = mcAttrInfoList.owner;
			// 			this.power = mcAttrInfoList.power;
			// 			this.professionalId = mcAttrInfoList.professionalId;
			// 			this.serialNum = mcAttrInfoList.serialNum;
			// 			this.stationName = mcAttrInfoList.stationName;
			// 			this.systemName = mcAttrInfoList.systemName;
			// 			this.voltage = mcAttrInfoList.voltage;
			// 		}else{
			// 			uni.showToast({
			// 				title:"数据列表请求失败...",
			// 				icon:"none"
			// 			});
			// 		}
			// 	}
			// });
		},
		methods: {
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			//首次时间
			bindFristDateChange: function(e) {
				this.maintainFristTime = e.target.value
			},
			//本次时间
			bindThisDateChange: function(e) {
				this.maintain_this_time = e.target.value
			},
			//下次时间
			bindNextDateChange: function(e) {
				this.maintainNextTime = e.target.value
			},
			//保修日期
			guaranteePeriodDateChange: function(e) {
				this.guaranteePeriod = e.target.value
			},
			createdTimeChange:function(e){
				this.createdTime = e.target.value
			},
			dateOfProductionDateChange:function(e){
				this.dateOfProduction = e.target.value
			},
			installDateChange: function(e) {
				this.installDate = e.target.value
			},
			isPlanChange:function(e){
				if(e.target.value == 0){
					this.flag = false;
				}else if (e.target.value == 1){
					this.flag = true;
				}
			},
			//表单
			formSubmit: function(e) {
				//表单数据校验
				console.log(rulesEdit);
				var rule = rulesEdit;
				var formData = e.detail.value;
				var checkflg = graceCheckerJs.check(formData, rule);
				console.log("submit>>>"+formData.etCode);
				// formData = JSON.parse(e.detail.value);
				if (!checkflg) {
					uni.showToast({
						title: graceCheckerJs.error,
						icon: "none"
					});
				} else {
					uni.showLoading({
						mask: true,
						title: '更新中...'
					});
					
					
					this.$myRequest({
						url:'/equipmentLAIM/saveOrUpdateAttrValue',
						data: {
							eid:this.eid,//ID
							etCode:this.etCode,
							domain:this.domain,
							systemName:this.systemName,
							etName:this.etName,
							etType:this.etType,
							etComment:this.etComment,
							createdId:this.createdId,
							createdName:this.createdName,
							createdTime:this.createdTime,
							owner:this.owner,
							etModel:this.etModel,
							etSupplier:this.etSupplier,
							etProducer:this.etProducer,
							dateOfProduction:this.dateOfProduction,
							guaranteePeriod:this.guaranteePeriod,
							serialNum:this.serialNum,
							professionalId:this.professionalId,
							voltage:this.voltage,
							power:this.power,
							life:this.life,
							pzLine:{lineId:this.lineId,lineName:this.stationName},
							stationName:this.stationName,
							installLocation:this.installLocation,
							installDate:this.installDate,
							
						},
						method:"POST"
					}).then((res) => {
						uni.hideLoading();
						if(res.data.code == 0){
							uni.showToast({
								title: "更新成功！",
								icon: "success",
								duration: 2000
							});
							setTimeout(function() {
								uni.redirectTo({
									url:"equipmentResume?editResult=success"
								})
							}, 2000);
							// const eventChannel = this.getOpenerEventChannel()
							// eventChannel.emit('editSubmited', {data: 'success'});
							// uni.navigateBack({
							//     delta: 1
							// });
						}else{
							uni.showToast({
								title: "更新失败..."+res.data.message,
								icon: "none",
								duration: 2000
							});
							setTimeout(function() {
								uni.redirectTo({
									url:"equipmentResume?editResult=success"
								})
							}, 2000);
							// const eventChannel = this.getOpenerEventChannel()
							// eventChannel.emit('editSubmited', {data: 'error'});
							// uni.navigateBack({
							//     delta: 1
							// });
						}
					});
					
					// uni.request({
					//     url: 'http://127.0.0.1:10100/equipmentLAIM/saveOrUpdateAttrValue', //仅为示例，并非真实接口地址。
					//     data: {
					// 		eid:this.eid,//ID
					// 		etCode:this.etCode,
					// 		domain:this.domain,
					// 		systemName:this.systemName,
					// 		etName:this.etName,
					// 		etType:this.etType,
					// 		etComment:this.etComment,
					// 		createdId:this.createdId,
					// 		createdName:this.createdName,
					// 		createdTime:this.createdTime,
					// 		owner:this.owner,
					// 		etModel:this.etModel,
					// 		etSupplier:this.etSupplier,
					// 		etProducer:this.etProducer,
					// 		dateOfProduction:this.dateOfProduction,
					// 		guaranteePeriod:this.guaranteePeriod,
					// 		serialNum:this.serialNum,
					// 		professionalId:this.professionalId,
					// 		voltage:this.voltage,
					// 		power:this.power,
					// 		life:this.life,
					// 		pzLine:{lineId:this.lineId,lineName:this.stationName},
					// 		stationName:this.stationName,
					// 		installLocation:this.installLocation,
					// 		installDate:this.installDate,
							
					//     },
					// 	method:"POST",
					//     success: (res) => {
					// 		uni.hideLoading();
					// 		if(res.data.code == 0){
					// 			uni.showToast({
					// 				title: "更新成功！",
					// 				icon: "success",
					// 				duration: 2000
					// 			});
					// 			setTimeout(function() {
					// 				uni.redirectTo({
					// 					url:"equipmentResume?editResult=success"
					// 				})
					// 			}, 2000);
					// 			// const eventChannel = this.getOpenerEventChannel()
					// 			// eventChannel.emit('editSubmited', {data: 'success'});
					// 			// uni.navigateBack({
					// 			//     delta: 1
					// 			// });
					// 		}else{
					// 			uni.showToast({
					// 				title: "更新失败..."+res.data.message,
					// 				icon: "none",
					// 				duration: 2000
					// 			});
					// 			setTimeout(function() {
					// 				uni.redirectTo({
					// 					url:"equipmentResume?editResult=success"
					// 				})
					// 			}, 2000);
					// 			// const eventChannel = this.getOpenerEventChannel()
					// 			// eventChannel.emit('editSubmited', {data: 'error'});
					// 			// uni.navigateBack({
					// 			//     delta: 1
					// 			// });
					// 		}
					//     },
					// 	fail: (res) => {
					// 		uni.showToast({
					// 			title: "请求失败..."+res.data.message,
					// 			icon: "none",
					// 			duration: 2000
					// 		});
					// 		setTimeout(function() {
					// 			uni.redirectTo({
					// 				url:"equipmentResume?editResult=success"
					// 			})
					// 		}, 2000);
					// 		// const eventChannel = this.getOpenerEventChannel()
					// 		// eventChannel.emit('editSubmited', {data: 'error'});
					// 		// uni.navigateBack({
					// 		//     delta: 1
					// 		// });
					// 	}
					// });
				}
			},
			formReset: function(e) {
				console.log('数据清空触发...');
			},
			infoMoreUpd:function(){
				if(this.disabledStr == true){
					this.disabledStr = false;
					this.updBtnStr = "取消编辑";
				}else{
					this.disabledStr = true;
					this.updBtnStr = "详情编辑";
				}
				
			}
		}
	}
</script>